package com.fh.fruit.controller.account;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.fruit.entity.account.CashApply;
import com.fh.fruit.entity.account.UserReal;
import com.fh.fruit.service.account.AccountManager;
import com.fh.util.Const;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author joe
 */
@Slf4j
@Controller
@RequestMapping(value = "account")
public class AccountController extends BaseController {
    @Resource(name = "accountService")
    private AccountManager accountManager;


    /**
     * 显示提现列表
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "cashApply/listAllClassify")
    public ModelAndView listAllClassify(Model model, String id) throws Exception {
        ModelAndView mv = this.getModelAndView();
        try {
            mv.setViewName("farm/account/cashout_index");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * 提现列表
     *
     * @param page 需要包含state（00,01,02）
     * @return 商品列表
     * @throws Exception
     */
    @RequestMapping(value = "cashApply/list")
    public ModelAndView listCashout(Page page, HttpSession httpSession) throws Exception {
        ModelAndView mv = this.getModelAndView();
        PageData pd = this.getPageData();

        page.setPd(pd);
        page.setShowCount(5);

        List<CashApply> cashApplies = accountManager.listCashout(page);
        mv.setViewName("farm/account/cashout_list");
        mv.addObject("cashApplies", cashApplies);
        mv.addObject("pd", pd);
        mv.addObject("state", pd.get("state"));
        mv.addObject("QX", Jurisdiction.getHC());    //按钮权限
        return mv;
    }

    /**
     * 跳转编辑拒绝提现申请原因的页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "cashApply/editReject")
    public ModelAndView editCashoutReject(Page page) throws Exception {
        ModelAndView mv = this.getModelAndView();
        PageData pd = this.getPageData();
        mv.setViewName("farm/account/cashapply_edit");
        mv.addObject("pd", pd);
        mv.addObject("id", pd.get("id"));
        return mv;
    }

    /**
     * 审核提现
     *
     * @param ids    提现申请ID的数组
     * @param state  01表示同意，02表示拒绝
     * @param remark 如果state是02那么有该字段，表示拒绝原因
     * @return 返回审核状态，返回true表示审核成功
     */
    @RequestMapping(value = "cashApply/checkCashout")
    @ResponseBody
    public boolean checkCashout(@RequestParam(value = "ids[]") long[] ids, String state, String remark) {
        Session session = Jurisdiction.getSession();
        try {
            User user = (User) session.getAttribute(Const.SESSION_USER);
            String uid = user.getUSER_ID();
            log.debug("审核员{}{}id{}的提现申请", uid, "01".equals(state) ? "同意" : "拒绝", ids);
            return accountManager.checkCashout(ids, uid, state, remark);
        } catch (Exception e) {
            log.error("提现审批失败", e);
            return false;
        }
    }

    /**
     * 显示提现列表
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "cert/listAllClassify")
    public ModelAndView listAllCertApply(Model model, String id) throws Exception {
        ModelAndView mv = this.getModelAndView();
        try {
            mv.setViewName("farm/account/cert_index");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * 实名认证列表
     *
     * @param page 需要包含state（00,01,02）
     * @return 商品列表
     * @throws Exception
     */
    @RequestMapping(value = "cert/list")
    public ModelAndView listCert(Page page, HttpSession httpSession) throws Exception {
        ModelAndView mv = this.getModelAndView();
        PageData pd = this.getPageData();

        page.setPd(pd);
        page.setShowCount(5);
        Object state = pd.get("state");

        List<UserReal> cashApplies = accountManager.listCert(page);
        mv.setViewName("farm/account/cert_list");
        mv.addObject("certList", cashApplies);
        mv.addObject("pd", pd);
        mv.addObject("state", state == null ? "01" : state);
        mv.addObject("QX", Jurisdiction.getHC());    //按钮权限
        return mv;
    }

    /**
     * 跳转编辑拒绝实名认证申请原因的页面
     *
     * @return 页面
     * @throws Exception
     */
    @RequestMapping(value = "cert/editReject")
    public ModelAndView editCertReject(Page page) throws Exception {
        ModelAndView mv = this.getModelAndView();
        PageData pd = this.getPageData();
        mv.setViewName("farm/account/cert_edit");
        mv.addObject("pd", pd);
        mv.addObject("id", pd.get("id"));
        return mv;
    }

    /**
     * 审核提现
     *
     * @param ids    实名认证申请ID的数组
     * @param state  02表示同意，03表示拒绝
     * @param remark 如果state是03那么有该字段，表示拒绝原因
     * @return 返回审核状态，返回true表示审核（包括同意或者拒绝）成功
     */
    @RequestMapping(value = "cert/checkCert")
    @ResponseBody
    public boolean checkCert(@RequestParam(value = "ids[]") long[] ids, String state, String remark) {
        Session session = Jurisdiction.getSession();
        try {
            User user = (User) session.getAttribute(Const.SESSION_USER);
            String uid = user.getUSER_ID();
            log.debug("审核员{}{}id{}的实名认证", uid, "02".equals(state) ? "同意" : "拒绝", ids);
            return accountManager.checkCert(ids, uid, state, remark);
        } catch (Exception e) {
            log.error("提现审批失败", e);
            return false;
        }
    }

}
